package com.mebs.model;

import javax.persistence.*;
import java.util.Date;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com
 *
 * Date: Jun 1, 2006, 3:43:30 PM
 */
@Embeddable
public class AuditInfo {
    @ManyToOne(optional = false)
    @JoinColumn(name = "AuthTraceUserId")
    @Field(type = ArgumentType.INSTANCE)
    private User authTraceUser;

    @Column(name = "AuthTraceDate", nullable = false)
    private Date authTraceDate;

    @ManyToOne(optional = false)
    @JoinColumn(name = "CreatedByUserId")
    @Field(type = ArgumentType.INSTANCE)
    private User createdByUser;

    @Column(name = "CreatedByDate", nullable = false)
    private Date createdByDate;

    public User getAuthTraceUser()
    {
        return authTraceUser;
    }

    public void setAuthTraceUser(User authTraceUser)
    {
        this.authTraceUser = authTraceUser;
    }

    public Date getAuthTraceDate()
    {
        return authTraceDate;
    }

    public void setAuthTraceDate(Date authTraceDate)
    {
        this.authTraceDate = authTraceDate;
    }

    public User getCreatedByUser()
    {
        return createdByUser;
    }

    public void setCreatedByUser(User createdByUser)
    {
        this.createdByUser = createdByUser;
    }

    public Date getCreatedByDate()
    {
        return createdByDate;
    }

    public void setCreatedByDate(Date createdByDate)
    {
        this.createdByDate = createdByDate;
    }

    public void auditTouch(User user)
    {
        auditTouch(user, new Date());
    }

    public void auditTouch(User user, Date date)
    {
        setAuthTraceUser(user);
        setAuthTraceDate(date);

        if ((createdByDate == null) || (createdByUser == null)) {
            setCreatedByDate(getAuthTraceDate());
            setCreatedByUser(getAuthTraceUser());
        }
    }
}
